package cn.dgut.changeManager;

import cn.dgut.Utils.JDBCUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.sql.Connection;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

@WebServlet("/ModifyInfo")
public class ModifyInfoServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("UTF-8");
        Map<String, String[]> parameterMap = request.getParameterMap();
        List<String> list = new ArrayList<>();

        for (Iterator iter = parameterMap.entrySet().iterator(); iter.hasNext(); ) {
            Map.Entry element = (Map.Entry) iter.next();
            //key值
            //Object strKey = element.getKey();
            //value,数组形式
            String[] value = (String[]) element.getValue();
            //System.out.print(strKey.toString() + "=");
            for (int i = 0; i < value.length; i++) {
                //System.out.print(value[i] + ",");
                list.add(value[i]);
            }
        }
        String isSell = request.getParameter("isSell");
        String content = request.getParameter("content");
        
        int page = Integer.parseInt(list.get(0));
        String sql = null;
        if(page==0){
            sql = "update User set user_name='"+list.get(2)+"',user_passwd='"+list.get(3)+"',user_phone='"+
                    list.get(4)+"',user_email='"+list.get(5)+"' where user_id="+list.get(1);
        }else if(page==1){
            sql = "update Student set student_name='"+list.get(2)+"',student_passwd='"+list.get(3)+"',student_phone='"+
                    list.get(4)+"',student_email='"+list.get(5)+"',isVip='"+list.get(6)+"',slevel='"+list.get(7)+"' where student_id = "+list.get(1);
        }else if(page==3){
            if(content!=null) {
                sql = "update Course set course_name='" + list.get(2) + "',tid=" + list.get(3) + ",hours=" +
                        list.get(4) + ",type='" + list.get(5) + "',course_begin='" + list.get(6) + "',course_pay=" + list.get(7) + ",cinfo='" + content + "',isSell=" + isSell + " where course_id = " + list.get(1);
            }else {
                sql = "update Course set course_name='" + list.get(2) + "',tid=" + list.get(3) + ",hours=" +
                        list.get(4) + ",type='" + list.get(5) + "',course_begin='" + list.get(6) + "',course_pay=" + list.get(7)  + ",isSell=" + isSell + " where course_id = " + list.get(1);
            }
        }else if(page==4){
            sql = "update Select_Course set course_id="+list.get(2)+",sid="+list.get(3)+",time='"+list.get(4)+"' where id="+list.get(1);
        }else if(page==5){
            if(content!=null) {
                sql = "update StudentWorks set student_name='"+list.get(2)+"',content='"+content+"' where StudentWorks_id="+list.get(1);
            }else {
                sql = "update StudentWorks set student_name='"+list.get(2)+"' where StudentWorks_id="+list.get(1);
            }
        }else if(page==6){
            if(content!=null) {
                sql = "update Recruit set title='" + list.get(2) + "',phone='" + list.get(3) + "',wage=" + list.get(4) + ",rinfo='" + content + "' where recruit_id=" + list.get(1);
            }else {
                sql = "update Recruit set title='" + list.get(2) + "',phone='" + list.get(3) + "',wage=" + list.get(4) + " where recruit_id=" + list.get(1);
            }
        }else if(page==7||page==2){
            if(content!=null){
                sql = "update Teacher set teacher_name='"+list.get(2)+"',teacher_passwd='"+list.get(3)+"',teacher_phone='"+
                        list.get(4)+"',teacher_email='"+list.get(5)+"',tlevel='"+list.get(6)+"',wage="+list.get(7)+",tinfo='"+content+"' where teacher_id = "+list.get(1);
            }else {
                sql = "update Teacher set teacher_name='"+list.get(2)+"',teacher_passwd='"+list.get(3)+"',teacher_phone='"+
                        list.get(4)+"',teacher_email='"+list.get(5)+"',tlevel='"+list.get(6)+"',wage="+list.get(7)+" where teacher_id = "+list.get(1);
            }
        }else {
            return;
        }
        JDBCUtils jdbcUtils = new JDBCUtils();
        int status = jdbcUtils.querySQL(sql);
        HttpSession session = request.getSession(true);
        session.setAttribute("isRight", status);
        response.sendRedirect("/backstage/main_page.jsp?page="+page);
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request,response);
    }
}